/*
Copyright (c) 2008 Stefan Lange-Hegermann

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/

// check if mootools is loaded
if (!MooTools) {
	throw "MooTools required for tacolib!";
}
// check for a compatible mootools version
if (!['1.2.0','1.2.1'].contains(MooTools['version'])) {
	throw "tacolib does not support MooTools version "+MooTools['version']+"!";
}

// tacolib is defined AFTER the mootools checks to make sure it can be loaded
var tacolib = {
	version:  '0.1a1',
	build:    '0',
	authors:  ['Stefan Lange-Hegermann'],
	license:  'BSD'
};

window.addEvent('domready', function() {
	var path_parts = window.location.pathname.split('\/');
	var file_name = path_parts[path_parts.length-1];
	var file_title = file_name.split('\.')[0];
	
	var page = {
		filename:  path_parts[path_parts.length-1],
		filetitle: file_name.split('\.')[0],
		pagehash:  file_title+'Page',
		pagetitle: window.document.title
	};
	
	// delete to remove from memory before running the pages initialize function
	delete path_parts;
	delete file_name;
	delete file_title;
	
	var pageData=window[page['pagehash']];
	
	if ($type(pageData) !== 'undefined') {
		if ($type(pageData.actions)==='array') {
			pageData.actions.each(function (action) {
				$$(action['element']).addEvent(action['event'], action['call'].bindWithEvent());
			})
		}
		
		if ($type(pageData.hover)==='array') {
			pageData.hover.each(function (action) {
				var element=$$(action['element']);
				element.addEvent('mouseenter', function () {
					element.addClass(action['className'] || 'hover');
				});
				
				element.addEvent('mouseleave', function () {
					element.removeClass(action['className'] || 'hover');
				});
			})
		}
		
		if ($type(pageData.initialize)==='function')
			pageData.initialize(tacolib, page);
		
		if ($type(pageData.precache)==='object') {};

	}
});

tacolib.Template=new Class({
		initialize:function(template) {
			this.template=template;
		},
		
		fill:function(data) {
			data=$H(data);
			
			result=this.template.replace(/%{([^}]+)}/ig, function(val, val2){
				return data.get(val2);
			}.bind(this));
			return result;
		}
});
